package cn.rengy.web.framework.sysenum;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.rengy.web.framework.cache.CacheService;
import cn.rengy.web.framework.sql.DbService;
@Service
public class SysCodeService {
	private static Logger logger = LoggerFactory.getLogger(SysCodeService.class);
	@Autowired
	private DbService dbService;
	
	@Autowired
	private CacheService cacheService;
	
	/*@Cacheable(value="testCache",key="#key1")
	public List<Map<String, Object>> test1(String key1){
		logger.error("已执行");
		List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
		Map<String, Object> m=new HashMap<String,Object>();
		list.add(m);
		return list;
	}*/
	public List<Map<String, Object>> queryBySqlname(String codeKey){
		return this.dbService.queryForList("select_sub_info1");
	}
	//@Cacheable(value="sysCodeCache",key="#codeKey")
	public List<Map<String, Object>> query(String codeKey){
		return cacheService.getLocalCache("sysCodeCache", codeKey, new Callable<List<Map<String, Object>>>() {
			
			@Override
			public List<Map<String, Object>> call() throws Exception {
				List<Map<String, Object>> list= dbService.queryForList("select_sys_code_bykey", codeKey);
				return list;
			}
			
		});
		
		
		
		/*if(list!=null){
			map=new HashMap<String,Object>();
			map.put("list",list);
			Map<String,Object> codeMap=new HashMap<String,Object>();
			for(Map<String, Object> codemap:list){
				codeMap.put((String)codemap.get("code_value"), codemap.get("code_name"));
			}
			map.put("map",codeMap);
		}
		return map;*/
	}
	
	public static String getCodeName(String codeValue,List<Map<String, Object>> codeList){
		String codeName=codeValue;
		if(codeList!=null){
			for(Map<String, Object> code:codeList){
				String code_value = (String)code.get("code_value");
				String code_name = (String)code.get("code_name");
				if(code_value!=null&&code_value.equals(codeValue)){
					codeName=code_name;
					break;
				}
			}
		}
		return codeName;
	}
	public static String getCodeNamev2(String codeValue,List<Map<String, Object>> codeList){
		String codeName=codeValue;
		if(codeList!=null){
			for(Map<String, Object> code:codeList){
				String code_value = (String)code.get("value");
				String code_name = (String)code.get("text");
				if(code_value!=null&&code_value.equals(codeValue)){
					codeName=code_name;
					break;
				}
			}
		}
		return codeName;
	}
	/*@Cacheable(value="tableCodeCache",key="#tablename")
	public Map<String,List<Map<String, Object>>> queryTableCode(String tablename){
		Map<String,List<Map<String, Object>>> map=null;
		List<Map<String, Object>> columnList=this.dbService.queryListBySqlId("select_code_by_tablename", tablename);
		if(columnList!=null){
			map=new HashMap<String,List<Map<String, Object>>>();
			for(Map<String, Object> columnMap:columnList){
				String column=(String)columnMap.get("table_column");
				String codeKey=(String)columnMap.get("code_key");
				List<Map<String, Object>> codeList=queryList(codeKey);
				map.put(column,codeList);
			}
		}
		return map;
	}*/
}
